package com.haina.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haina.shop.domain.*;

import com.haina.shop.mapper.*;
import com.haina.shop.service.ProdService1;
import com.haina.shop.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author 48077
* @description 针对表【tz_prod(商品)】的数据库操作Service实现
* @createDate 2024-07-31 18:34:29
*/
@Service
public class ProdServiceImpl1 extends ServiceImpl<ProdMapper, Prod>
    implements ProdService1 {

    @Autowired
    private ProdMapper prodMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private TransportMapper transportMapper;
    @Autowired
    private TransfeeMapper transfeeMapper;
    @Autowired
    private TransfeeFreeMapper transfeeFreeMapper;



    @Override
    public InfoVo getInfo(Long prodId)
    {//
        InfoVo infoVo = new InfoVo();
      // long prodId=infoVo.getProdId();
//        InfoDto infoDto = new InfoDto();
//        infoDto.setProdId(prodId);
        Prod prod = prodMapper.selectById(prodId);
        infoVo.setProdId(prodId);
        infoVo.setProdName(prod.getProdName());
        infoVo.setShopId(prod.getShopId());
        infoVo.setOriPrice(prod.getOriPrice());
        infoVo.setPrice(prod.getPrice());
        infoVo.setBrief(prod.getBrief());
        infoVo.setContent(prod.getContent());
        infoVo.setPic(prod.getPic());
        infoVo.setImgs(prod.getImgs());
        infoVo.setImgs(prod.getImgs());
        infoVo.setTotalStocks(prod.getTotalStocks());
        List<Sku> skus = skuMapper.selectByProdId(prodId);



        List<SkuVo1> skuVo1 = new ArrayList<SkuVo1>();
        for (Sku sku1 : skus) {
            SkuVo1 skuVo = new SkuVo1();
            skuVo.setSkuId(sku1.getSkuId());
            skuVo.setPic(sku1.getPic());
            skuVo.setProperties(sku1.getProperties());
            skuVo.setSkuName(sku1.getSkuName());
            skuVo.setPrice(sku1.getPrice());
            skuVo.setStocks(sku1.getStocks());
            skuVo.setOriPrice(sku1.getOriPrice());
            skuVo1.add(skuVo);
        }
        infoVo.setSkuList(skuVo1);

        TransportVo1 transportVo = new TransportVo1();
        Transport transport = transportMapper.selectByfree();
        transportVo.setTransportId(transport.getTransportId());
        transportVo.setTransName(transport.getTransName());
        transportVo.setCreateTime(transport.getCreateTime());
        transportVo.setShopId(transport.getShopId());
        transportVo.setChargeType(transport.getChargeType());
        transportVo.setIsFreeFee(transport.getIsFreeFee());
        transportVo.setHasFreeCondition(transport.getHasFreeCondition());



        System.out.println(transport.getTransportId());
        List<TransFeeFreeVO1> list1=new ArrayList<>();
        if (transport.getTransportId()==47){
            transportVo.setTransfeeFrees(null);
        }else {
            TransfeeFree transfeeFree = transfeeFreeMapper.selectByTransportId(transport.getTransportId());
            List<TransfeeFree> transfeeFreeList = Arrays.asList(transfeeFree);

            for ( TransfeeFree transfeeFree1 : transfeeFreeList) {
                TransFeeFreeVO1 transfeeFreeVO = new TransFeeFreeVO1();
                transfeeFreeVO.setTransfeeFreeId(transfeeFree1.getTransfeeFreeId());
                transfeeFreeVO.setTransportId(transfeeFree1.getTransportId());
                transfeeFreeVO.setFreeType(transfeeFree1.getFreeType());
                transfeeFreeVO.setAmount(transfeeFree1.getAmount());
                transfeeFreeVO.setPiece(transfeeFree1.getPiece());
                list1.add(transfeeFreeVO);
            }
        }


        Transfee transfee = transfeeMapper.selectByTransportId(transport.getTransportId());
        List<Transfee> transfeeList = Arrays.asList(transfee);
        List<TransfeeVo1> list=new ArrayList<>();
        List<AreaVO1> areas = new ArrayList<>();
        for (Transfee transfee1 : transfeeList) {
            TransfeeVo1 transfeeVo = new TransfeeVo1();
            transfeeVo.setTransfeeId(transfee1.getTransfeeId());
            transfeeVo.setTransportId(transfee1.getTransportId());
            transfeeVo.setContinuousPiece(transfee1.getContinuousPiece());
            transfeeVo.setFirstPiece(transfee1.getFirstPiece());
            transfeeVo.setContinuousFee(transfee1.getContinuousFee());
            transfeeVo.setFirstFee(transfee1.getFirstFee());
            transfeeVo.setFreeCityList(areas);
            list.add(transfeeVo);
        }

        transportVo.setTransfeeList(list);
        transportVo.setTransfeeFrees(list1);
        infoVo.setTransportList(transportVo);

        return infoVo;
    }

}




